﻿using log4net;
using System;
using System.Collections.Concurrent;
using System.Reflection;
using WpfApp1.Acme.Template;

namespace WpfApp1.Acme.ApplicationClass {
    /// <summary>
    /// 数据源上下文负责对数据源策略的注册与获取
    /// </summary>
    internal class DataSourceContext {
        /** 数据源策略池 */
        private static ConcurrentDictionary<string, object> straytegyMap = new ConcurrentDictionary<string, object>();
        /** 日志对象 */
        private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// 获取策略
        /// </summary>
        /// <param name="straytegyName">策略名称</param>
        /// <returns></returns>
        public static DataSourceOperation getStraytegy(string straytegyName) {
            DataSourceOperation dataSourceOperation = null;
            try {
               dataSourceOperation = (DataSourceOperation)straytegyMap[straytegyName];
            } catch (Exception e) { 
                logger.Error(e);
            }
            return dataSourceOperation; 
        }
        /// <summary>
        /// 注册策略
        /// </summary>
        /// <param name="straytegyName">策略名称</param>
        /// <param name="straytegyClass">策略类</param>
        public static void registStraytegy(string straytegyName, object straytegyClass) { 
            straytegyMap.TryAdd(straytegyName, straytegyClass);
        }
    }
}
